import Vue from 'vue'
import VueRouter from 'vue-router'
// import HomeView from '../views/HomeView.vue'
import Login from '../views/Login.vue'
import CoreLogin from '../components/CoreLogin.vue'
import CoreRegister from '../components/CoreRegister.vue'
import SportsShop from '../views/SportsShop.vue'
import Goods from '../components/Goods.vue'
import Orders from '../components/Orders.vue'
import ShopCar from '../components/ShopCar.vue'
import Settings from '../components/Settings.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '',
    redirect:"/login"
  },
  {
    path: '/login',
    // name: 'Login',
    component: Login,
    children: [
      {
        path: '/',
        redirect:"coreLogin"
      },
      {
        path: 'coreLogin',
        component: CoreLogin
      },
      {
        path: 'coreRegister',
        component: CoreRegister
      }
    ]
  },
  {
    path:'/sportsShop',
    // name: 'SportsShop',
    component:SportsShop,
    children: [
      {
        path: '/',
        redirect:"goods"
      },
      {
        path: 'goods',
        component: Goods
      },
      {
        path: 'shopCar',
        component: ShopCar
      },
      {
        path: 'orders',
        component: Orders
      },
      {
        path: 'settings',
        component: Settings
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 路由导航守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    return next()
  }
  if (to.path === '/login/coreLogin') {
    return next()
  }
  if (to.path === '/login/coreRegister') {
    return next()
  }

  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) {
    return next('/login')
  }

  return next()

})

export default router
